#!/usr/bin/env python
# encoding: utf-8

from math import log

def normalize_dict(d):
    total = sum(d.values())
    if total == 0:
        return
    for k in d:
        d[k] = d[k] / total

def normalize_list(v):
    total = sum(v)
    if total == 0:
        return
    for i in range(len(v)):
        v[i] = v[i] / total

def bm25(idf, tf, fl, avgfl, B=0.75, K1=1.5):
    return idf*((tf * (K1 + 1)) / (tf + K1 * ((1 - B) + B * fl / avgfl)))

def normalized_google_distance(A, B, div=1):
    a = len(set(A).intersection(B)) / div
    b = max(len(A), len(B)) / div
    c = min(len(A), len(B)) / div
    if c == 0 or b == 0:
        return 1.0
    if a == 0:
        return -1
    numerator = log(b) - log(a)
    denominator = 13.495564213452393 - log(c)
    result = numerator / denominator
    #if result > 1.0 :
    #    print('Error: b:', log(b), '\ta:', log(a), '\tc:', log(c))
    #    print('Error: num:', numerator, '\tden:', denominator)
    return result

if __name__ == '__main__':
    print(normalized_google_distance([1,2,3], [1,2,4]))

